package mvp.com.bbc.rcjavaretrofitdaggermvp.baseAF;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${cbf} on 2017/12/2.
 */

public abstract class BaseAdapter extends RecyclerView.Adapter<BaseAdapter.Vhloder> {
    private List<Object> objects;
    private Context context;

    public BaseAdapter(Context context) {
        this.context = context;
        objects = new ArrayList<>();
        notifyDataSetChanged();
    }

    public void setData(List<Object> data) {
        this.objects = data;
        notifyDataSetChanged();
    }

    public void addData(List<Object> data) {
        this.objects.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public BaseAdapter.Vhloder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(layoutadapter(), parent, false);
        return new Vhloder(view);
    }


    @Override
    public void onBindViewHolder(BaseAdapter.Vhloder holder, int position) {
        layoutData(objects, position);
    }


    @Override
    public int getItemCount() {
        return objects.size();
    }

    public class Vhloder extends RecyclerView.ViewHolder {
        public Vhloder(View itemView) {
            super(itemView);
            layoutFindViewById(itemView);
        }
    }

    //提供基类的一个方法出去给子类实现
    protected abstract int layoutadapter();

    //提供基类的一个方法出去给子类实现
    protected abstract void layoutData(List<Object> objects, int position);

    //提供基类的一个方法出去给子类实现
    protected abstract void layoutFindViewById(View itemView);
}
